package com.kgc.ebookmanagement2.controller;

import com.github.pagehelper.PageInfo;
import com.kgc.ebookmanagement2.pojo.Ebook_Entry;
import com.kgc.ebookmanagement2.pojo.ResultEntity;
import com.kgc.ebookmanagement2.service.Ebook_EntryService;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @author 陈啸
 * @create 2020-05-19 15:26
 */
@RestController
public class EntryController {
    @Resource
    Ebook_EntryService entryService;

    @RequestMapping("/selectEntry")
    public ResultEntity<PageInfo<Ebook_Entry>> selectEntry(@RequestParam(value = "categoryId", required = false, defaultValue = "0") Integer categoryId,
                                                           @RequestParam(value = "pageNum", required = false, defaultValue = "1") Integer pageNum,
                                                           @RequestParam(value = "pageSize", required = false, defaultValue = "2") Integer pageSize) {
        PageInfo<Ebook_Entry> pageInfo = entryService.getSelect(pageNum, pageSize, categoryId);
        return ResultEntity.successWithoutData(pageInfo);
    }

    //前端post请求，参数是json必须添加@RequestBody
    //添加和修改
    @RequestMapping("/saveEntry")
    public ResultEntity<String> saveEntry(@RequestBody Ebook_Entry entry) {
        try {
            Integer integer = 0;
            if (entry.getId() == null) {
                integer = entryService.add(entry);
            } else {
                integer = entryService.update(entry);
            }
            return ResultEntity.successWithoutData(integer+"");
        }catch (Exception ex){
            return ResultEntity.successWithoutData(ex.getMessage());
        }
    }
    //根据id查询
    @RequestMapping("/getEntryId")
    public ResultEntity<Ebook_Entry> getEntryId(Integer id) {
        Ebook_Entry entry = entryService.selectById(id);
        return ResultEntity.successWithoutData(entry);
    }

    //根据id删除
    @RequestMapping("/delEntryId")
    public ResultEntity<String> delEntryId(Integer id) {
        try{
            Integer integer = entryService.delete(id);
            return ResultEntity.successWithoutData(integer+"");
        }catch (Exception ex){
            return ResultEntity.failed(ex.getMessage());
        }
    }

}
